﻿using Newtonsoft.Json;
using Sharp.Redis;
using SharpDB;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace RedisConsoleApp
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            RedisCache.RemoveAll();

            //Test();

            List<Person> lstper = new List<Person>()
            {
                new Person(){ Name="xiaoqiang",Age=20},
                new Person(){ Name="wangwu",Age=21},
                new Person(){ Name="xiaogang",Age=22},
                new Person(){ Name="admin",Age=23}
            };

            string json = JsonConvert.SerializeObject(lstper);

            for (int j = 0; j < 5; j++)
            {
                RedisCache.Set("persons" + j, lstper);
            }

            List<string> lstkeys = RedisCache.SearchKeys("persons*");

            RedisCache.Set("aaaa", "abc");
            long lg = RedisCache.AppendToValue("aaaa", "def");
            string val = RedisCache.Get<string>("aaaa");
            string val2 = RedisCache.GetValue("aaaa");

            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("a", "1");
            dict.Add("b", "2");
            dict.Add("c", "3");
            RedisCache.SetValues(dict);

            var lstval2 = RedisCache.GetValues();
            var lstval = RedisCache.GetValues("a", "b", "c");

            List<string> lst999 = null;
            RedisCache.Set("a", lst999);
            lst999 = RedisCache.Get<List<string>>("a");

            RedisCache.GetAllKeys();
           

            Console.ReadKey();

        }

        private static void Test()
        {
            DB db = new DB(AccessType.MsSql);

            string sql_txt = @"select distinct COMPANY_CODE AS code,COMPANY_FULL_NAME AS name from M_PANEL_COMPANY
SELECT distinct G0QYGYS AS code,GYSMS AS name from M_GYS
SELECT DISTINCT G0QYWL AS code, G0QYWLCWB AS name FROM M_WL
SELECT DISTINCT MSEHI AS name ,MSEHL AS name from M_UNIT
SELECT DISTINCT G0WLYDLX, TXTSH from M_WLYDLX
 select DISTINCT YJLB AS code,YJLBMC AS name from M_YJLBMC
select DISTINCT G0ZBWLDL AS code, G0ZBWLDL_LONG_DESC AS name from M_ZBWLDL
select DISTINCT G0XSQD AS code ,TXTLG AS name from M_FXQD
select DISTINCT G0XSZZ AS code, TXTLG AS name from M_XSZZ
select DISTINCT BM AS code,MC AS name from M_KHZHZ
select DISTINCT G0JYZMS AS code, TXTLG AS name from M_JYZMS
select DISTINCT G0ZBKH AS code ,TXTLG AS name from M_MDMKH
select DISTINCT G0XDKZFW AS code, TXTLG AS name from M_XDKZFW
select DISTINCT G0XSPZLB AS code,TXTLG AS name from M_XSPZLB
select DISTINCT BM AS code, TXTMD AS name from M_CLZT
select DISTINCT G0XKHFXLB AS code ,TXTMD AS name from M_FXLB
select DISTINCT G0GC AS code, TXTMD AS name from M_GC
select DISTINCT G0QYKH AS code ,TXTMD AS name from M_KH
select DISTINCT G0CPZ AS code, TXTSH AS name from M_CPZ
select DISTINCT G0WLDWSX AS code,TXTSH AS name FROM M_WLDWSX
select DISTINCT G0XSDDLX AS code, TXTSH AS name from M_XSDJLX
select DISTINCT GZP000007 AS code ,TXTSH AS name from M_JYZDYYYZT";

            string[] sqls = sql_txt.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            Stopwatch wat = new Stopwatch();
            foreach (var sql in sqls)
            {
                string tabName = Regex.Replace(sql, @".+from\s(.+)", "$1", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                string code = string.Empty, name = string.Empty;
                try
                {
                    wat.Restart();
                    long lg = 0;
                    DbDataReader reader = db.ExecuteReader(sql);
                    while (reader.Read())
                    {
                        code = reader["code"].ToString();
                        name = reader["name"].ToString();

                        RedisCache.Hash_SetIfNotExists(tabName, code, name);
                        lg++;
                    }
                    wat.Stop();
                    Console.WriteLine("当前表" + tabName + ",共有" + lg + "条数据，插入使用：" + wat.Elapsed.TotalSeconds + "s");
                }
                catch (Exception ex)
                {
                    Thread.Sleep(1000);
                    continue;
                }
            }
            Console.WriteLine("执行完毕！按任意键结束。。。");
        }
    }
}
